﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Threading;


namespace TagFlo
{
    public class ThreadedObservableCollection<CollectionType> : ObservableCollection<CollectionType> //where CollectionType : Type
    {
    




            private Dispatcher dispatcherUIThread;
            private delegate void AddCallback(CollectionType item);
            private delegate void RemoveCallback(CollectionType item);
            private delegate void ClearCallback();


            public ThreadedObservableCollection(Dispatcher dispatcher)
                : base()
            {
                this.dispatcherUIThread = dispatcher;
            }

            public ThreadedObservableCollection(Dispatcher dispatcher, List<CollectionType> list)
                : base(list)
            {
                this.dispatcherUIThread = dispatcher;
            }


            new public void Add(CollectionType item)
            {
                if (dispatcherUIThread.CheckAccess())
                {
                    base.Add(item);
                }
                else
                {
                    dispatcherUIThread.Invoke(DispatcherPriority.Send, new AddCallback(Add), item);
                }
            }

            new public void Remove(CollectionType item)
            {
                if (dispatcherUIThread.CheckAccess())
                {
                    base.Remove(item);
                }
                else
                {
                    dispatcherUIThread.Invoke(DispatcherPriority.Send, new RemoveCallback(Remove), item);
                }
            }

            new public void Clear()
            {
                if (dispatcherUIThread.CheckAccess())
                {
                    base.Clear();
                }
                else
                {
                    dispatcherUIThread.Invoke(DispatcherPriority.Send, new ClearCallback(Clear));
                }
            }

           

        
    }
}
